export class TableKeyBoadrExtend {
  isMultiSelect = false; // 单复选状态记录
  prevRow = null; // 缓存shift前置行
  selectRows = []; // 选中的行
  singleTableRef = null // 表格组件对象
  tableData = []
  constructor(tabRef, tableData, selectRows) {
    this.singleTableRef = tabRef
    this.tableData = tableData
    this.selectRows = selectRows
  }
  // 清空选中行
  clearSelectHandle() {
    this.selectRows = []
    this.isMultiSelect = false
    this.singleTableRef?.clearSelection()
  }
  // 表格样式
  rowClassNameFun({ row }) {
    return this.selectRows.indexOf(row) !== -1 ? 'selected' : ''
  }
  // 行点击
  rowClickHandle(row, column, event) {
    // 当行对象有selectable属性，并且属性值为false，就不可选中当前行 & 非shift组合模式，可以完成shift终点选择
    if (row.selectable === false && !window.$keymaster.shift) { return false }
    // 点击的是选项列，触发复选选中效果
    if (column?.className === 'el-table-column--selection') {
      if (!this.isMultiSelect && this.selectRows.length === 1 && this.selectRows[0] === row) {
        this.isMultiSelect = true
        return false
      } else {
        this.singleTableRef.toggleRowSelection(row)
      }
      let selectRowH = this.singleTableRef.getSelectionRows()
      this.selectHandle(selectRowH, row, true) // 添加第三个标志位，区分是否是table的checkbox直接触发，true不是，false是
      this.isMultiSelect = selectRowH.length > 0
      if (selectRowH.length === 0) { this.prevRow = null } // 置前行为空
      return false
    }
    // 按下ctrl键的同时，触发row的点击事件，保持复选
    if (window.$keymaster.ctrl) {
      this.singleTableRef.toggleRowSelection(row)
      let selectRowH = this.singleTableRef.getSelectionRows()
      this.selectHandle(selectRowH, row, true)
      this.isMultiSelect = selectRowH.length > 0
    } else if (window.$keymaster.shift) {
      this.isMultiSelect = true
      // 按下shift键的同时，触发row行点击事件，与上一次操作行之间的区域选中
      // 先清空所有选中行
      this.singleTableRef.clearSelection()
      let ind1 = 0
      let ind2 = this.tableData.findIndex(item => item == row)
      if (this.prevRow !== null) {
        ind1 = this.tableData.findIndex(item => item === this.prevRow)
      }
      // 选中起始到结束
      let start = Math.min(ind1, ind2)
      let end = Math.max(ind1, ind2)
      for (let i = start; i <= end; i++) {
        let cur = this.tableData[i]
        // 当前行selectable如果不是false，就执行选中
        if (cur.selectable !== false) {
          this.singleTableRef.toggleRowSelection(this.tableData[i], true)
        }
      }
      // 父子项级联处理+右侧操作栏目按钮联动
      let curRows = this.singleTableRef.getSelectionRows()
      this.selectHandle(curRows, row, true)
    } else {
      // 当前仅选中一行，并且点击的行就是选中行时不做处理
      if (this.selectRows.length === 1 && row === this.selectRows[0]) {
        this.singleTableRef.toggleRowSelection(row, false)
        this.isMultiSelect = false
        this.selectHandle([], row, true)
        return false
      } else {
        this.isMultiSelect = false
      }
      // 正常单选流程
      this.singleTableRef.clearSelection()
      this.singleTableRef.toggleRowSelection(row, true)
      let curRows = this.singleTableRef.getSelectionRows()
      this.selectHandle(curRows, row, true)
    }
  }
  // 行选中处理
  selectHandle(selection, row, funCall) {
    if (!funCall) {
      // 当前是单选状态，并且选项为空时，不让它取消选中效果，并进入复选状态
      if (!this.isMultiSelect && selection.length === 0) {
        this.singleTableRef.toggleRowSelection(row, true)
        this.isMultiSelect = true
      }
      let curRows = this.singleTableRef.getSelectionRows()
      this.isMultiSelect = curRows.length > 0
      if (curRows.length === 0) { this.prevRow = null } // 置前行为空
    }
    // flag为true，为选中，false为取消选中
    let flag = selection.indexOf(row) !== -1
    // 记录下当前行
    if (flag) { this.prevRow = row }
    // 记录选中的行
    this.selectRows = this.singleTableRef.getSelectionRows() || []
  }
  // 全选处理
  selectAllHandle() {
    let selection = this.singleTableRef.getSelectionRows() || []
    if(this.tableData.length === 1 && !this.isMultiSelect) {
      this.isMultiSelect = true
    } else {
      let isSelectAll  = selection.length === this.tableData.length
      if(selection.length > 1) {
        this.isMultiSelect = true
      } else {
        this.isMultiSelect = isSelectAll
      }
    }
    this.prevRow = null
    this.selectRows = this.singleTableRef.getSelectionRows() || []
  }
  selectOneHandle(row) {
    this.singleTableRef.toggleRowSelection(row, true)
    let curRows = this.singleTableRef.getSelectionRows()
    this.isMultiSelect = curRows.length > 0
    this.selectRows = this.singleTableRef.getSelectionRows() || []
  }
  doSelectAll() {
    this.tableData.forEach(row => {
      this.singleTableRef.toggleRowSelection(row, true)
    })
    this.isMultiSelect = true
    this.selectRows = this.singleTableRef.getSelectionRows() || []
  }
  doSelectFirstRow() {
    this.clearSelectHandle()
    if(this.tableData.length) {
      this.singleTableRef.clearSelection()
      this.singleTableRef.toggleRowSelection(this.tableData[0], true)
      let curRows = this.singleTableRef.getSelectionRows()
      this.selectHandle(curRows, this.tableData[0], true)
    }
  }
}